﻿using Face8280WebSocketServer.Command;
using DoNetDrive.Core.Command;
using DoNetDrive.Core.Connector;
using DoNetDrive.Core.Packet;
using DotNetty.Buffers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace Face8280WebSocketServer.WebSockt
{
    public class WebSocketRequestHandle : AbstractRequestHandle
    {
        public WebSocketRequestHandle() : base(new JsonPacketDecompile())
        {

        }

        public override void DisposeResponse(INConnector connector, IByteBuffer msg)
        {

        }

        protected override void fireRequestEvent(INConnector connector, INPacket p)
        {
            JsonPacket packet = p as JsonPacket;
            var json = packet.GetJson();
            try
            {

                var oJson = JObject.Parse(json);
                var cmd = oJson.Property("msgtype", StringComparison.OrdinalIgnoreCase);
                if (cmd != null)
                {
                    WebSocketTransaction transaction = new WebSocketTransaction(connector.GetConnectorDetail(), json);
                    connector.FireTransactionMessage(transaction);
                }
            }
            catch
            {
                Console.WriteLine("消息接收：接收到异常消息！:" + json);
            }

        }
    }
}
